package mybatis;/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. All rights reserved.
 */

import org.opengauss.Logger;
import org.opengauss.intercept.DataRecordFactory;
import org.opengauss.intercept.DoRecord;
import org.opengauss.intercept.DoRecord.PairRecord;
import org.opengauss.mapper.BatchInsertMapper;
import org.opengauss.service.FactoryInstance;
import org.opengauss.vo.BatchInsertVo;
import common.testcase.BaseMyBatisTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;

/**
 * description:this for JdbcBatchTest Class
 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2021.
 *
 * @author Administrator
 * @version [openGauss_debug 0.0.1 2021/3/15]
 * @since 2021/3/15
 */
public class MybatisProviderTest extends BaseMyBatisTestCase<BatchInsertMapper> {
//    private static final String MYBATIS_ENV = "mysql_druid";
    private static final String MYBATIS_ENV = "druid";
    @Before
    public void setUp() throws IOException {
        initSession();
        mapper.createTable();
        initAutoId(mapper);
    }

    @After
    public void tearDown() throws SQLException {
        mapper.dropTable();
        closeSession();
    }

    @Test
    public void test() throws InterruptedException {
        System.out.println("just a test!");
        BatchInsertVo vo = new BatchInsertVo();
        vo.id = autoId.getAndIncrement();
        vo.data = "this is a test " + vo.id;
        int num = mapper.add(vo);
        assertEquals(1, num);
        Thread.sleep(10000);
        initSession();
        BatchInsertVo test1 = mapper.getById(vo.id);
        assertEquals(vo.id, test1.id);
    }
    
    @Test
    public void test11() throws InterruptedException {
        System.out.println("just a test11!");
        BatchInsertVo vo = new BatchInsertVo();
        vo.id = autoId.getAndIncrement();
        vo.data = "this is a test " + vo.id;
        int num = mapper.add(vo);
        
        List<BatchInsertVo> tests = mapper.test111("test");
        for (BatchInsertVo tmpVo: tests) {
            System.out.println("" + tmpVo);
        }
    }
    
    @Override
    protected void initSession() {
        try {
            factory = FactoryInstance.INSTANCE.getDruidSessionFactory(MYBATIS_ENV);
            sqlSession = factory.openSession(true);
            mapper = getMapper();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    @Test
    public void test1() throws InterruptedException {
        final int INSERT_COUNT = 1000;
        for (int i = 0; i < INSERT_COUNT; i++) {
            BatchInsertVo vo = new BatchInsertVo();
            vo.id = autoId.getAndIncrement();
            vo.data = "this is a test " + vo.id;
            getMapper().add(vo);
        }
        long testTime = 1000 * 60 * 3;
//        long testTime = 1000 * 10 * 1;
        long pre = System.currentTimeMillis();
        Random random = new Random(pre);
        AtomicBoolean flag = new AtomicBoolean(true);
        Thread th = new Thread(() -> {
            long[] results = new long[2];
            PairRecord[] maxArray = new PairRecord[DoRecord.MAX_ARRAY];
            PairRecord[] minArray = new PairRecord[DoRecord.MAX_ARRAY];
            DoRecord[] records = {DataRecordFactory.getExectorRecord(), DataRecordFactory.getStmtRecord(),
            DataRecordFactory.getParamRecord(), DataRecordFactory.getResultRecord()};
            while (flag.get()) {
                Logger.info(String.format("------------begin-----"));
                for (DoRecord doRecord: records) {
                    Logger.info(String.format("-------do it---" + doRecord));
                    doRecord.getRecord(results, maxArray, minArray);
                    Logger.info(String.format("total=%d, count=%d", results[0], results[1]));
                    Logger.info(String.format("max=%s", Arrays.stream(maxArray).map(val -> "" + val).collect(Collectors.joining(","))));
                    Logger.info(String.format("min=%s", Arrays.stream(minArray).map(val -> ""+val).collect(Collectors.joining(","))));
                }
                Logger.info(String.format("------------end-----"));
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        th.start();
        while (System.currentTimeMillis() - pre < testTime) {
            int findId = random.nextInt(INSERT_COUNT);
            try {
                BatchInsertVo vo = getMapper().getById(findId);
                Logger.info("we find:" + vo.toInsertData());
                Thread.sleep(10);
            } catch (Exception exp) {
                Logger.info("we find:" + findId);
                throw exp;
            }
        }
        flag.set(false);
        th.join();
        Logger.info("execute success!");
//        try {
//            System.in.read();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    @Override
    public BatchInsertMapper getMapper() {
        return getMapper(BatchInsertMapper.class);
    }

    @Override
    public int getMaxId(BatchInsertMapper mapper) {
        return mapper.maxId();
    }
}
